package com.ruoyi.basicsystem.controller;

import com.alibaba.fastjson.JSON;
import com.ruoyi.basicsystem.domain.ZnAgreementCustomer;
import com.ruoyi.basicsystem.domain.ZnAgreementDetail;
import com.ruoyi.basicsystem.domain.ZnSupplier;
import com.ruoyi.basicsystem.service.IZnSupplierService;
import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.utils.SecurityUtils;

import java.util.*;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.basicsystem.domain.ZnAgreement;
import com.ruoyi.basicsystem.service.IZnAgreementService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 客户协议价管理Controller
 * 
 * @author nexiaozi
 * @date 2022-03-25
 */
@RestController
@RequestMapping("/basicsystem/agreement")
public class ZnAgreementController extends BaseController
{
    @Autowired
    private IZnAgreementService znAgreementService;

    @Autowired
    private IZnSupplierService znSupplierService;
    /**
     * 查询客户协议价管理列表
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:agreement:list')")
    @GetMapping("/detailList")
    public AjaxResult detailList(ZnAgreement znAgreement)
    {
        System.out.println(JSON.toJSONString(znAgreement));
        if(znAgreement.getParams().containsKey("materialIds")){
            String materialIdsStr =  znAgreement.getParams().get("materialIds").toString();
            znAgreement.getParams().put("materialIds",materialIdsStr.split(","));
        }
        System.out.println(JSON.toJSONString(znAgreement));
        List<ZnAgreementDetail> list = znAgreementService.selectZnAgreementDetailList(znAgreement);
        return AjaxResult.success(list);
    }
    /**
     * 查询客户协议价管理列表
     */
    @GetMapping("/selectCustomerListByAgreementId/{id}")
    public AjaxResult selectCustomerListByAgreementId(@PathVariable("id") Long id)
    {
        ZnAgreement znAgreement = new ZnAgreement();
        znAgreement.setDeleteFlag("0");
        znAgreement.setTenantId(SecurityUtils.getTenantId());
        znAgreement.setParentId(id);
        List<ZnAgreement> list = znAgreementService.selectZnAgreementList(znAgreement);
        return AjaxResult.success(list);
    }
    /**
     * 查询客户协议价管理列表
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:agreement:list')")
    @GetMapping("/selectlist")
    public AjaxResult selectlist(ZnAgreement znAgreement)
    {
        Map<String,Object> params =new HashMap<>();
//        params.put("status",new String[]{"1","2"});
        znAgreement.setStatus("1");
        znAgreement.setParams(params);
        znAgreement.setParentId(0l);
        znAgreement.setDeleteFlag("0");
        znAgreement.setTenantId(SecurityUtils.getTenantId());
        List<ZnAgreement> list = znAgreementService.selectZnAgreementList(znAgreement);
        return AjaxResult.success(list);
    }
    /**
     * 查询客户协议价管理列表
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:agreement:list')")
    @GetMapping("/list")
    public AjaxResult list(ZnAgreement znAgreement)
    {
        List<ZnAgreement> list = znAgreementService.selectZnAgreementList(znAgreement);
        return AjaxResult.success(list);
    }

    /**
     * 导出客户协议价管理列表
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:agreement:export')")
    @Log(title = "客户协议价管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ZnAgreement znAgreement)
    {
        znAgreement.setTenantId(SecurityUtils.getTenantId());
        List<ZnAgreement> list = znAgreementService.selectZnAgreementList(znAgreement);
        ExcelUtil<ZnAgreement> util = new ExcelUtil<ZnAgreement>(ZnAgreement.class);
        util.exportExcel(response, list, "客户协议价管理数据");
    }

    /**
     * 获取客户协议价管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:agreement:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        ZnAgreement znAgreement = znAgreementService.selectZnAgreementById(id);
//        System.out.println(JSON.toJSONString(znAgreement));
        return AjaxResult.success(znAgreement);
    }

    /**
     * 新增客户协议价管理
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:agreement:add')")
    @Log(title = "客户协议价管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ZnAgreement znAgreement)
    {
//        System.out.println(JSON.toJSONString(znAgreement));
        return toAjax(znAgreementService.insertZnAgreement(znAgreement));
    }
    /**
     * 修改客户协议价管理
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:agreement:edit')")
    @Log(title = "客户协议价管理", businessType = BusinessType.UPDATE)
    @PutMapping("/updateCustomerAgreement")
    public AjaxResult updateCustomerAgreement(@RequestBody ZnAgreement znAgreement)
    {
        znAgreement.setTenantId(SecurityUtils.getTenantId());
        znAgreement.setDeleteFlag("0");
        return toAjax(znAgreementService.updateCustomerAgreement(znAgreement));
    }
    /**
     * 修改客户协议价管理
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:agreement:edit')")
    @Log(title = "客户协议价管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ZnAgreement znAgreement)
    {
        return toAjax(znAgreementService.updateZnAgreement(znAgreement));
    }

    /**
     * 删除客户协议价管理
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:agreement:remove')")
    @Log(title = "客户协议价管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        ZnAgreement znAgreement = new ZnAgreement();
        znAgreement.setStatus("2");
        Map<String,Object> params = new HashMap<>();
        params.put("parentId",ids);
        znAgreement.setParams(params);
        znAgreement.setDeleteFlag("0");
        znAgreement.setTenantId(SecurityUtils.getTenantId());
        int count = znAgreementService.selectZnAgreementCount(znAgreement);
        int deleteCount = 0;
        if(count  ==0){
            deleteCount = znAgreementService.deleteZnAgreementByIds(ids);
        }else{
            return AjaxResult.error("删除协议中，已生效无法直接删除");
        }
        return toAjax(deleteCount);
    }


    /**
     * 修改客户协议价状态
     */
//    @PreAuthorize("@ss.hasPermi('basicsystem:agreement:status')")
    @Log(title = "客户协议价管理", businessType = BusinessType.UPDATE)
    @GetMapping("/doChangeStatus/{status}/{ids}")
    public AjaxResult changeStatus(@PathVariable String status,@PathVariable Long[] ids)
    {
        return toAjax(znAgreementService.updateZnAgreementStatusByIds(status,ids));
    }
    /**
     * 设置客户协议价管理
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:agreement:edit')")
    @Log(title = "客户协议价管理", businessType = BusinessType.UPDATE)
    @PutMapping("/doCustomer")
    public AjaxResult doCustomer(@RequestBody ZnAgreementCustomer znAgreementCustomer)
    {
        ZnAgreement znAgreement = znAgreementService.selectZnAgreementById(znAgreementCustomer.getAgreementId());
        if(znAgreement == null){
            return AjaxResult.error("该协议已不存在");
        }

        return toAjax(znAgreementService.doCustomer(znAgreementCustomer));
    }
//    /**
//     * 修改客户协议价管理
//     */
//    @PreAuthorize("@ss.hasPermi('basicsystem:agreement:edit')")
//    @Log(title = "客户协议价管理", businessType = BusinessType.UPDATE)
//    @GetMapping("/doCustomer/{agreementId}/{customerIds}")
//    public AjaxResult doCustomer(@PathVariable Long agreementId,@PathVariable Long[] customerIds)
//    {
//        ZnAgreement znAgreement = znAgreementService.selectZnAgreementById(agreementId);
//        if(znAgreement.getStatus().equals("2")){
//            return AjaxResult.error("该协议已生效不能修改用户");
//        }
//        ZnAgreement znAgreement1 = null;
//        for (Long customerId:customerIds) {
//            znAgreement1 = new ZnAgreement();
//            znAgreement1.setCustomerId(customerId);
//            znAgreement1.setTenantId(SecurityUtils.getTenantId());
//            znAgreement1.setDeleteFlag("0");
//            Map<String,Object> params = new HashMap<>();
////            params.put("status",new String[]{"0","1"});
//            params.put("status","0,1");
//            params.put("parentId",agreementId);
//            params.put("effectTime",znAgreement.getEffectTime());
//            params.put("invalidTime",znAgreement.getInvalidTime());
//            znAgreement1.setParams(params);
//           List<Map<String,Object>> list = znAgreementService.selectCustomerHaveAgreement(znAgreement1);
//           if(list.size() >0){
//               return AjaxResult.error(list.get(0).get("customerName").toString() + "已设置关联"+list.get(0).get("title").toString());
//           }
//        }
//
//        return toAjax(znAgreementService.doCustomer(agreementId,customerIds));
//    }
    /**
     * 修改客户协议价管理
     */
    @PreAuthorize("@ss.hasPermi('basicsystem:agreement:edit')")
    @Log(title = "客户协议价管理", businessType = BusinessType.UPDATE)
    @GetMapping("/doCustomer/{agreementId}")
    public AjaxResult doCustomer(@PathVariable Long agreementId)
    {
        ZnAgreement znAgreement = znAgreementService.selectZnAgreementById(agreementId);
        if(znAgreement.getStatus().equals("2")){
            return AjaxResult.error("该协议已生效不能修改用户");
        }

        return toAjax(znAgreementService.doCustomer(agreementId,null));
    }
}
